package com.ychs.servlet;

import com.ychs.entity.College;
import com.ychs.entity.Major;
import com.ychs.service.IBaseService;
import com.ychs.service.MajorService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/7/19
 */
@WebServlet(value = "/MajorServlet")
public class MajorServlet extends BaseServlet {

    IBaseService<Major> service = new MajorService();

    @Override
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter major add");
        String collegeId = request.getParameter("college");
        String name = request.getParameter("name");
        String code = request.getParameter("code");

        Major major = new Major();
        major.setName(name);
        major.setCode(Integer.parseInt(code));
        College college = new College();
        college.setId(Integer.parseInt(collegeId));
        major.setCollege(college);

        boolean result = service.add(major);
        logger.info(result);

        response.getWriter().print(result);
    }

    @Override
    protected void modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter modify method");
        String id = request.getParameter("id");
        String collegeId = request.getParameter("college");
        String name = request.getParameter("name");
        String code = request.getParameter("code");

        Major major = new Major();
        major.setId(Integer.parseInt(id));
        major.setName(name);
        major.setCode(Integer.parseInt(code));
        College college = new College();
        college.setId(Integer.parseInt(collegeId));
        major.setCollege(college);

        boolean result = service.modify(major);
        logger.info(result);

        response.getWriter().print(result);
    }

    @Override
    protected void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter remove method");
        String id = request.getParameter("id");
        boolean result = service.remove(Integer.parseInt(id));
        logger.info(result);
        response.getWriter().print(result);
    }

    @Override
    protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter major search");
        //院系TODO
        Map<String, Object> conditions = new HashMap<>();

        String collegeSearch = request.getParameter("collegeSearch");
        logger.info(collegeSearch);
        //用常量去equals字符串对象，避免空指针
        if(collegeSearch!=null&&!"".equals(collegeSearch)){
            conditions.put("college", collegeSearch);
        }

        int count = service.searchTotalNumber(conditions);

        String page = request.getParameter("page");
        String limit = request.getParameter("limit");

        if (page != null && limit != null) {
            conditions.put("begin", (Integer.parseInt(page) - 1) * Integer.parseInt(limit));
            conditions.put("pageSize", Integer.parseInt(limit));
        }else{
            conditions.put("begin", 0);
            //2是为了方便测试，一般为10
            conditions.put("pageSize", 10);
        }
        List<Major> majors = service.search(conditions);
        logger.info(majors);
        String json = json(count, majors);
        logger.info(json);
        response.getWriter().print(json);
    }

    @Override
    protected void searchById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter searchById");
        String id = request.getParameter("id");
        Major major = service.searchById(Integer.parseInt(id));
        String json = json(major);
        logger.info(json);
        response.getWriter().print(json);
    }
}
